package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.Classroom;
import com.example.studentstatusmanagement.pojo.ClassroomExample;
import com.example.studentstatusmanagement.pojo.Major;
import com.example.studentstatusmanagement.pojo.MajorExample;
import com.example.studentstatusmanagement.pojo.doo.ReqClassroom;
import com.example.studentstatusmanagement.pojo.doo.ReqMajor;
import com.example.studentstatusmanagement.pojo.dto.SMajor;
import com.example.studentstatusmanagement.service.ClassroomService;
import com.example.studentstatusmanagement.service.MajorService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api/v1/classroom")
@RestController
public class ClassroomController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private ClassroomService classroomService;

    @RequestMapping("showmajor")
    public ResultResponse showmajor() {
        return ResultResponse.Build(ResultTypeEnum.success, majorService.selectMajorsforClass());
    }


    @RequestMapping("show")
    public Map<String, Object> showMajorsInfo(ReqClassroom reqClassroom) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SMajor> ls = classroomService.selectClassroosInfo(reqClassroom);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }


    @RequestMapping("del")
    public ResultResponse del(Classroom classroom) {
        if (!StringUtils.isEmpty(classroom.getId())) {
            classroom.setIsDel(SystemVariable.IS_DEL);
            if (classroomService.updateByPrimaryKeySelective(classroom) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("use")
    public ResultResponse use(Classroom classroom) {
        if (!StringUtils.isEmpty(classroom.getId())) {
            classroom.setIsDel(SystemVariable.IS_NOT_DEL);
            if (classroomService.updateByPrimaryKeySelective(classroom) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("update")
    public ResultResponse update(Classroom classroom) {

        ClassroomExample example = new ClassroomExample();
        ClassroomExample.Criteria c = example.createCriteria();
        c.andClassNameEqualTo(classroom.getClassName());
        c.andMajorIdEqualTo(classroom.getMajorId());
        c.andIdNotEqualTo(classroom.getId());
        if (classroomService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (!StringUtils.isEmpty(classroom.getId())) {
            if (classroomService.updateByPrimaryKeySelective(classroom) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("add")
    public ResultResponse add(Classroom classroom) {

        ClassroomExample example = new ClassroomExample();
        ClassroomExample.Criteria c = example.createCriteria();
        c.andClassNameEqualTo(classroom.getClassName());
        c.andMajorIdEqualTo(classroom.getMajorId());

        if (classroomService.selectByExample(example).size() > 0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (classroomService.insertSelective(classroom) == SystemVariable.SUCCESS) {
            return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
}
